package com.xx.ems.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.xx.ems.dao.EquipmentReceiveDao;
import com.xx.ems.model.Equipments;
import com.xx.ems.model.Receive;
import com.xx.ems.model.ReceiveApply;
import com.xx.ems.model.ReceiveNum;
import com.xx.ems.model.Users;
import com.xx.ems.system.BaseDao;

@Repository(value="equipmentReceiveDao")
@Transactional
public class EquipmentReceiveDaoImpl extends BaseDao implements EquipmentReceiveDao {
	@Override
	public void receiveEquipment(String[] eidArray, int uid, Users user) {
		/**
		 * 获取领用人对象
		 */
		Users receiveUser = (Users) getCurrentSession().get(Users.class, uid);
		/**
		 * 获取领用单号
		 */
		String rnum = getEquipmentBase().generateCode();
		/**
		 * 创建领用单号对象并设值
		 */
		ReceiveNum receiveNum = new ReceiveNum();
		receiveNum.setRnum(rnum);
		receiveNum.setBusers(user);
		receiveNum.setUsers(receiveUser);
		receiveNum.setBorrow_time(new Date());
		/**
		 * 将领用人与领用的设备进行关联
		 */
		for (String eid : eidArray) {
			/**
			 * 获取领用设备对象
			 */
			Equipments equipment = (Equipments) getCurrentSession().get(Equipments.class, Integer.valueOf(eid));
			/**
			 * 创建领用对象并设值
			 */
			Receive receive = new Receive();
			receive.setEquipments(equipment);
			/**
			 * 设置领用单号对象与领用对象的关联
			 */
			receiveNum.getReceive().add(receive);
		}
		/**
		 * 保存领用单号对象
		 */
		getCurrentSession().save(receiveNum);
	}
	
	@Override
	public void restoreEquipment(String[] eidArray, Users user) {
		for (String eid : eidArray) {
			Receive receive = (Receive) getCurrentSession().createQuery("from Receive re where re.fk_eid = "+Integer.valueOf(eid)).uniqueResult();
			/**
			 * 设置归还操作人和归还时间
			 */
			receive.setRusers(user);
			receive.setReturn_time(new Date());
			getCurrentSession().update(receive);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getReceiveNumList(int pageNow, int maxCount) {
		/**
		 * 获取领用单列表
		 */
		String hql = "from ReceiveNum re order by re.borrow_time desc";
		List<ReceiveNum> receiveNumList = getPagingList(pageNow, maxCount, hql);
		List<Object> list = new ArrayList<Object>();
		for (ReceiveNum receiveNum : receiveNumList) {
			list.add(receiveNum.toHashMap());
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("totalpage", getTotalpage());
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getReceiveByReceiveNumId(int rid, int pageNow, int maxCount) {
		/**
		 * 获取领用信息
		 */
		String hql = "from Receive re where re.fk_rid = "+rid+" order by re.rid asc";
		List<Receive> receiveList = getPagingList(pageNow, maxCount, hql);
		List<Object> list = new ArrayList<Object>();
		for (Receive receive : receiveList) {
			list.add(receive.toHashMap());
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("totalpage", getTotalpage());
		return map;
	}
	
	public void addReceiveApply(ReceiveApply receiveApply) {
		receiveApply.setApply_status(0);
		getCurrentSession().save(receiveApply);
	}
}